In [1]:
%pylab inline
import numpy as np


Populating the interactive namespace from numpy and matplotlib

In [2]:
P = (0.25, 0.50)
idline = np.linspace(0,1,100)
# Identity line y = x, or alternatively Ax + By + C= 0, A =1, B = -1
plt.plot(idline, idline, '--', color='brown') # 
plt.plot(P[0], P[1], 'o', color = 'grey', markersize=8)


Out[2]:
[<matplotlib.lines.Line2D at 0x7f406d5f6950>]

Distance from a point to a line

\begin{equation} \frac{|Ax+By+C|}{\sqrt{A^2+B^2}}. \end{equation}

In [3]:
def distance(mypoint, myline):
    """
    Calculates the distance from a point to a line
    """
    
    x, y = mypoint
    
    A, B, C = myline
    
    return np.abs(A*x + B*y + C) / np.sqrt(np.power(A,2)+np.power(B,2))

In [4]:
mypoint = (5,1)
myline = (3,-1, 1)
distance(mypoint, myline) # 15/np.sqrt(10) = 4.743416


Out[4]:
4.7434164902525691

In [5]:
mypoint = (0.25, 0.50)
myline = (1, -1, 0) # identity line
distance(mypoint, myline)


Out[5]:
0.17677669529663687

Distance from a point the identity line

\begin{equation} \frac{|x - y|}{\sqrt{2}}. \end{equation}

In [6]:
def distance_idline(mypoint):
    """
    Calculates the distance from a point to the identity line
    """
    
    x, y = mypoint
    
    return np.abs(x-y) / np.sqrt(2)

In [7]:
myfoo = (0.5, 0.5)
distance_idline(myfoo) # must be zero


Out[7]:
0.0

In [8]:
distance_idline(mypoint) # must be 0.1767


Out[8]:
0.17677669529663687

In [9]:
mypoint= (0,1)
distance_idline(mypoint) #maximal separation is 1/sqrt(2)


Out[9]:
0.70710678118654746

In [10]:
1/np.sqrt(2) # voila!


Out[10]:
0.70710678118654746